// pages/home/device/setdeviceconfig.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cfg_id:0,
    cfg_name:"",
    cfg_unit:"",
    cfg_formula:"",
    cfg_enable:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _unit = options.unit == null || options.unit == "null" ? "" : options.unit;
    this.setData({ 
      cfg_id: options.id ,
      cfg_name: options.name,
      cfg_unit: _unit,
      cfg_formula: options.formula,
      cfg_enable: options.enable==1,
      });
    //console.log(this.data.cfg_enable);
  },
  listerner_name: function (e){
    this.setData({
      cfg_name: e.detail.value
    })
  },
  listerner_unit: function (e) {
    this.setData({
      cfg_unit: e.detail.value
    })
  },
  listerner_formula: function (e) {
    this.setData({
      cfg_formula: e.detail.value
    })
  },
  listerner_enable: function (e) {
    this.setData({
      cfg_enable: e.detail.value
    })
    //console.log(this.data.cfg_enable);
  },
  //修改设备配置
  ondeviceconfig: function (e) {
    var that = this;
    //console.log(this.data);
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/SetDeviceConfig", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        id: that.data.cfg_id,
        enable: that.data.cfg_enable,
        name: that.data.cfg_name,
        formula: that.data.cfg_formula,
        unit: that.data.cfg_unit,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var msg = "";
        if (res.data.result) {
          wx.navigateBack()
        }
        else {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
})